/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.encrypters;

import java.nio.ByteBuffer;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.NoSuchPaddingException;

import org.latestbit.smartobjects.io.ResizableByteBuffer;

/**
 * Interface provides access to all methods for asymmetric encryption processing
 * @author abdulla-a
 *
 */
public interface IAsymmetricDataEncrypter extends IDataEncrypter {

	/**
	 * Init algorithm with the specified private key
	 * @param key Private key
	 * @throws InvalidKeyException
	 * @throws InvalidKeySpecException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 */
	public void initCipher( PrivateKey key ) throws InvalidKeyException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException;
	
	/**
	 * Init algorithm with the specified public key
	 * @param key Private key
	 * @throws InvalidKeyException
	 * @throws InvalidKeySpecException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 */	
	public void initCipher( PublicKey key ) throws InvalidKeyException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException;

	/**
	 * Get current public key
	 * @return Public key
	 */
	public PublicKey getPublicKey();
	
	/**
	 * Get current private key
	 * @return Private key
	 */	
	public PrivateKey getPrivateKey();
	
	/**
	 * Decode serialized form of public key from byte buffer
	 * @param buffer Input byte buffer
	 * @return Public key
	 * @throws InvalidKeySpecException
	 * @throws NoSuchAlgorithmException
	 */	
	public PublicKey decodePublicKey(ByteBuffer buffer) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException;
	
	/**
	 * Encode public key to serialized form to output byte buffer
	 * @param key Input public key
	 * @param buffer Output byte buffer
	 */	
	public void encodePublicKey(PublicKey key, ResizableByteBuffer buffer );
	
	/**
	 * Generate key pair (public and private keys)
	 * @return Public and private keys
	 * @throws NoSuchAlgorithmException
	 */
	public KeyPair generateKeyPair() throws NoSuchAlgorithmException;
}
